package com.cxy.instarsystem.web;

import com.alibaba.fastjson.JSON;
import com.cxy.instarsystem.po.User;
import com.cxy.instarsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class registerController {

    @Autowired
    private UserService userService;

    @GetMapping("register")
    public String registerPage(){
        return "register";
    }


    @PostMapping("register")
    public String register(@RequestParam String username, @RequestParam String password, @RequestParam String email, User user){
        //保存之前判断用户名是否重名，前后端都要检验
        user.setNickname(username);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        userService.saveRegister(user);
        return "login";
    }

    @RequestMapping("isUsernameUnique")
    //转换json数据  @ResponseBody
    @ResponseBody
    public String isUsernameUnique(String username) {
        boolean byUsername = userService.isUsernameRepeat(username);
        return "{\"valid\":"+byUsername+"}";
    }
}
